<?php 
## include required files
/*******************************/
require_once '../model/users.php';
require_once '../model/moduleuser.php';
## Paginaton class
require_once ("../includes/classes/pagination_class.php");
/*******************************/
## Create Objects
/*******************************/
$userObj = new Model_Users();
$moduleObj=new Model_ModuleUser();
/*******************************/

/*******************************/
##Fetch all admin users from database
/******************************/
if($_GET['q']=='jobreview')
{
	$moduelArray=$moduleObj->getModuleHelpText(27);
	$smarty->assign('moduelArray', $moduelArray);
}


## Get search parameters in variables - 
if(isset($_POST['search']) != '' && $_POST['search'] != 'Homeowner Name') {
//	echo 'aaaaaa';exit;
	$searchindex = $_POST['search'];
} else if($_GET['search'] != '') {
	$searchindex = $_GET['search'];
} else {
	$searchindex = '';
}
// Assign search variable to show in search textbox
$smarty->assign('search', $searchindex);



if($_GET['col1'] != '') 
{
	$col1 = base64_decode($_GET['col1']);
} 
else if($_GET['col2'] != '') 
{
	$col2 = base64_decode($_GET['col2']);
} 



##  --------- Pagination part first start --------------##
	if( isset( $_GET[ 'page' ] ) && $_GET[ 'page' ] != "" ) {
		$pageNum = $_GET[ 'page' ];
	} else {
		$pageNum = 1;
	} 
	$rowsPerPage = ROW_PER_PAGE;
	
	if($_GET['col1'] != '') 
	{
		$pageName = "home.php?q=jobreview&col1=".$_GET['col1'].""; ;
	} 
	else if($_GET['col2'] != '') 
	{
		$pageName = "home.php?q=jobreview&col2=".$_GET['col2'].""; ;
	} 
	else 
	{
		$pageName = "home.php?q=jobreview"; 
	} 

	
	## Count all the records
	$reviewsArray = $userObj->getReviewByUserId($userid,$searchindex,$col1);
	$total_rows = count($reviewsArray);
	$offset	= ($pageNum - 1) * $rowsPerPage;
	$smarty->assign('page', $pageNum);
##  --------- Pagination part first end --------------##


##  --------- Pagination part second start --------------##
	$newid = $pageNum * $rowsPerPage - $rowsPerPage + 1;
	$smarty->assign('newid', $newid);
	$reviewsArray = $userObj->getReviewByUserId($userid,$searchindex,$col1,$rowsPerPage,$offset);
	//echo "<pre>"; print_r($reviewsArray);exit;
	
	for($k=0;$k<count($reviewsArray);$k++)
	{		
		if($reviewsArray[$k]['rating']!='0')
		{			
			$total_rating = $reviewsArray[$k]['rating'];
			$reviewsArray[$k]['total_rating'] =$total_rating;
			//$smarty->assign('total_rating', $total_rating);
			if($total_rating < 5){
				$remaining_rating = 5 - $total_rating;
				$reviewsArray[$k]['remaining_rating'] =$remaining_rating;
				//$smarty->assign('remaining_rating', $remaining_rating);
			} 
		}
		else
		{
			$reviewsArray[$k]['total_rating'] =0;	
			$reviewsArray[$k]['remaining_rating'] =5;
		}
		
		if($reviewsArray[$k]['to_id']!='0')
		{	
			$userDetArray = $userObj->getUserDetailsByUserId($reviewsArray[$k]['to_id']);
			$reviewsArray[$k]['to_username'] =$userDetArray['first_name']." ".$userDetArray['last_name'];
		}
	}	
	//echo"<pre>";print_R($reviewsArray);die();
	$smarty->assign('reviewsArray', $reviewsArray);
	
	if($searchindex != '') {
		$other_id = "search=".$searchindex;
	}
	else {
		$other_id = '';
	}
	
	if($total_rows > ROW_PER_PAGE) {
		$pg = new pagination();					// CREATING OBJECT OF THE PAGINATION CLASS
		$pgnation = $pg->pagination_admin( $total_rows , $rowsPerPage , $pageNum , $pageName , $other_id, $class);	
		$smarty-> assign("pagination",$pgnation);
	}
##  --------- Pagination part second end --------------##	


## Assign session message to smarty variable and unset session variable
if($_SESSION['msg'] != '') {
	$smarty->assign('msg', $_SESSION['msg']);	
	unset($_SESSION['msg']);
}

## Set active class variable for left menu  (main menu and sub menu)
$smarty->assign('activeclass', 'jobreview');
$smarty->assign('mainmenu', '7');

## Unset all the objects created which are on this page
unset($userObj);
unset($moduleObj);

$smarty->display(TEMPLATEDIR_ADMIN. 'review/jobreview.tpl');
unset($smarty);
?>